<template>
  <div class="app-container ">
    <el-form ref="form" size="mini" :model="form" :rules="rules" label-width="180px">
      <el-tabs v-model="activeName">
        <el-tab-pane style="padding-top:10px" label="城市代理设置" name="城市代理设置">
          <el-row>
            <el-col :span="24">
              <el-form-item label="城市代理默认返佣比例">
                <el-input v-model="form.return_percent" style="width:500px;" auto-complete="off" clearable placeholder="请输入默认佣金比例">
                  <template slot="append">%</template>
                </el-input>
                <div class="help-block">如果城市代理没有等级，佣金按这里的比例结算。</div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="最少提现额度">
                <el-input
                  v-model="form.min_money"
                  style="width:500px;"
                  auto-complete="off"
                  clearable
                  placeholder="请输入最少提现额度"
                >
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="每日提现上限">
                <el-input
                  v-model="form.cash_max_day"
                  style="width:500px;"
                  auto-complete="off"
                  clearable
                  placeholder="请输入每日提现上限"
                >
                  <template slot="append">元</template>
                </el-input>
                <div class="help-block">0元表示不限制每日提现金额</div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="限制城市下单">
                <el-radio-group v-model="form.is_limit">
                  <el-radio label="1">限制</el-radio>
                  <el-radio label="0">不限制</el-radio>
                </el-radio-group>
                <div class="help-block">如果设置为限制，只有开通城市代理的地区才可以下单</div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="提现手续费">
                <div style="margin-bottom: 8px;" v-for="(item, index) in form.cash_service_charge_list" :key="index">
                    <el-radio-group v-model="item.calculate_method">
                      <el-radio label="0">百分比</el-radio>
                      <el-radio label="1">固定金额</el-radio>
                    </el-radio-group>
                    <el-input v-model="item.title" style="width:180px;margin-left: 10px;margin-right: 3px;" auto-complete="off" clearable
                      placeholder="文字说明">
                    </el-input>
                    <el-input v-model="item.days" style="width:180px;margin-left: 10px;margin-right: 3px;" auto-complete="off" clearable
                      placeholder="请输入到帐天数">
                      <template slot="append">天到帐</template>
                    </el-input>
                    <el-input v-model="item.price" style="width:180px;margin-left: 10px;margin-right: 3px;" auto-complete="off" clearable
                      placeholder="提现手续费">
                      <template v-if="item.calculate_method=='0'" slot="append">%</template>
                      <template v-if="item.calculate_method=='1'" slot="append">元</template>
                    </el-input>
                    <el-button @click="addItem()">+</el-button>
                    <el-button @click="deleteItem(index)">–</el-button>

                </div>
                <div class="help-block">0表示不设置提现手续费
                  <span class="text-danger">提现手续费额外从提现中扣除</span><br>
                  例如：<span class="text-danger">10%</span>的提现手续费：<br>
                  提现<span class="text-danger">100</span>元，扣除手续费<span class="text-danger">10</span>元，
                  实际到手<span class="text-danger">90</span>元
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button size="mini" type="primary" @click="submit">保存设置</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>
<script>
export default {
  name: 'Configoperatingcity',
  components: {},
  data() {
    return {
      form: {
      },
      loading: false,
      activeName: '城市代理设置',
      rules: {}
    }
  },
  mounted() {
    this.$api.post('/config/getInfo', {
      mo: 'operatingcity'
    }).then(res => {
      this.form = JSON.stringify(res.data) == '[]' ? {} : res.data
      this.setDefaultVal('keyword')
    })
  },
  methods: {
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          this.$api.post('/config/update', this.form).then(res => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    setDefaultVal(key) {
      if (this.form[key] == null || this.form[key] == '') {
        this.form[key] = []
      }
    },
    addItem() {
      let that = this;
      that.form.cash_service_charge_list.push({
        days: "",
        calculate_method: '0',
        price: ""
      });
    },
    deleteItem(i) {
      if (this.form.cash_service_charge_list.length <= 1) {
        return false;
      }
      this.form.cash_service_charge_list.splice(i, 1);
    }
  }
}
</script>
